package com.banciyuan.bcywebview.biz.main.daily;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.space.Space;
import com.banciyuan.bcywebview.base.view.textview.FocusButton;
import com.banciyuan.bcywebview.biz.circles.CircleLikeHelper;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.TagDetail;

/**
 * Created by squall on 2016/4/29.
 */
public class DailyTagsAdapter extends BaseAdapter {

    private List<TagDetail> tlist = new ArrayList<>();
    private LayoutInflater inflater;
    private Context mContext;
    private ListView mlistView;


    public DailyTagsAdapter(List<TagDetail> tlist, Context mContext) {
        this.tlist = tlist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if ("work".equals(tlist.get(position).getType()))
            return 2;
        return TextUtils.isEmpty(tlist.get(position).getTag_name()) ? 0 : 1;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getCount() {
        return tlist.size();
    }

    @Override
    public Object getItem(int position) {
        return tlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        private ImageView iv_tag;
        private TextView tag_name;
        private TextView tag_intro;
        private FocusButton book_btn;

        public ViewHolder(View convertView) {
            iv_tag = (ImageView) convertView.findViewById(R.id.iv_tag);
            tag_name = (TextView) convertView.findViewById(R.id.tag_name);
            tag_intro = (TextView) convertView.findViewById(R.id.tag_intro);
            book_btn = (FocusButton) convertView.findViewById(R.id.book_btn);
        }
    }

    private class TitleHolder {
        private TextView title;
        private Space space;

        public TitleHolder(View convertView) {
            title = (TextView) convertView.findViewById(R.id.ground_zan_type_tv);
        }
    }

    public void setMlistView(ListView mlistView) {
        this.mlistView = mlistView;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        TitleHolder titleHolder = null;
        int itemViewType = getItemViewType(position);
        if (convertView == null) {
            if (itemViewType == 0) {
                convertView = inflater.inflate(R.layout.search_result_num, parent, false);
                titleHolder = new TitleHolder(convertView);
                convertView.setTag(titleHolder);
            } else {
                convertView = inflater.inflate(R.layout.daily_tag_item, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }
        } else {
            if (itemViewType == 0) {
                titleHolder = (TitleHolder) convertView.getTag();
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
        }
        final TagDetail t = tlist.get(position);
        if (itemViewType == 0) {
            if (!TextUtils.isEmpty(t.getTitle())) {
                titleHolder.title.setText(t.getTitle());
            }
            if (position == 0) {
                titleHolder.space.setVisibility(View.GONE);
            } else {
                titleHolder.space.setVisibility(View.VISIBLE);
            }
        } else {
            if (itemViewType == 1) {
                setTagName(viewHolder, t.getTag_name());
                setTagStatus(viewHolder, t.isTf_status());
                if (!TextUtils.isEmpty(t.getEvent_id())) {
                    viewHolder.tag_intro.setVisibility(View.GONE);
                } else {
                    viewHolder.tag_intro.setVisibility(View.VISIBLE);
                }
            } else {
                if (!TextUtils.isEmpty(t.getReal_name()))
                    viewHolder.tag_name.setText(t.getReal_name());
                setTagName(viewHolder, t.getReal_name());
                setTagStatus(viewHolder, t.isWf_status());
            }
            if (!TextUtils.isEmpty(t.getCover()))
                ImageLoader.getInstance().displayImage(t.getCover(), viewHolder.iv_tag, ImageOptions.options);
            else
                viewHolder.iv_tag.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tag_white));

            if (!TextUtils.isEmpty(t.getIntro()))
                viewHolder.tag_intro.setText(Html.fromHtml(t.getIntro()));
            doFocus(viewHolder, itemViewType, t);
            doItemClick(viewHolder, itemViewType, t);
        }
        return convertView;
    }

    private void doItemClick(ViewHolder viewHolder, final int itemViewType, final TagDetail t) {
        ((View) (viewHolder.iv_tag.getParent())).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemViewType == 1) {
                    if (TextUtils.isEmpty(t.getEvent_id())) {
                        Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                        intent.putExtra("type", "tag");
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                        mContext.startActivity(intent);
                    } else {
                        gotoUtil.gotoAct(mContext, EventDetailActivity.class, t.getEvent_id());
                    }
                } else {
                    Intent intent = new Intent(mContext, CircleSmoothActivity.class);
                    intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getReal_name());
                    intent.putExtra(gotoUtil.INTENT_VALUE_TWO, t.getId());
                    mContext.startActivity(intent);
                }
            }
        });
    }

    private void doFocus(ViewHolder viewHolder, final int itemViewType, final TagDetail t) {
        viewHolder.book_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                String type = itemViewType == 1 ? "tag" : "work";
                String id = itemViewType == 1 ? t.getTag_id() : t.getId();
                CircleLikeHelper.doCircleLike(mContext, id, type, "follow", new CircleLikeHelper.CircleLikeSuccess() {
                    @Override
                    public void onCircleLikeSuccess() {
                        if (itemViewType == 1) {
                            t.setTf_status(true);
                        } else {
                            t.setWf_status(true);
                        }
                        v.setSelected(!v.isSelected());
                    }
                });
            }
        });
    }

    private void setTagName(ViewHolder viewHolder, String name) {
        if (!TextUtils.isEmpty(name)) {
            viewHolder.tag_name.setText(name);
        }
    }

    private void setTagStatus(ViewHolder viewHolder, boolean isFocus) {
        if (!isFocus) {
            viewHolder.book_btn.setSelected(false);
        } else {
            viewHolder.book_btn.setSelected(true);
        }
    }
}
